package com.yy.base.common.dao;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import com.mes.entity.User;
import com.yy.base.common.entity.BaseEntity;
import com.yy.base.util.SessionInfo;
import com.yy.base.util.StringUtil;

@SuppressWarnings("unchecked")
public class SqlUtil {
	private List list = null;
	private String where = "";
	private Object whereValue = null;
	/**
	 * 执行处理
	 */
	public String createInsert(Object obj, String table) {
		StringBuilder sql = new StringBuilder();
		list = new ArrayList();
		sql.append("insert into ").append(table).append("(");
		Class clz = obj.getClass();
		if (!clz.equals(BaseEntity.class)) {
			BaseEntity entity = (BaseEntity)obj;
			entity.setCreated(new Date());
			
			User user = SessionInfo.getUser();
			if (user != null) {
				entity.setCreatedBy(user.getId());
			}
		}
		
		dg_createInsert(obj, clz, sql);
		
		sql.append(") values (");
		for (int i = 0, size = list.size(); i < size; i++) {
			if (i > 0) {
				sql.append(", ");
			}
			sql.append("?");
		}
		sql.append(")");
		return sql.toString();
	}
	/**
	 * 递归生成脚本
	 */
	private void dg_createInsert(Object obj, Class clz, StringBuilder sql) {
		if (clz.equals(Object.class)) {
			return;
		}
		Field[] fields = clz.getDeclaredFields();
		String name = null;
		Object value = null;
		Method md = null;
		
		for (Field field : fields) {
			name = field.getName();
			try {
				md = clz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
				if (md.isAnnotationPresent(Transient.class)) {
					continue;
				}
				if (md.isAnnotationPresent(Column.class)) {
					//判定是否定义了注解的字段名
					String columnName = md.getAnnotation(Column.class).name();
					if (columnName != null && !"".equals(columnName)) {
						name = columnName;
					}
				}
				value = md.invoke(obj);
				if (md.isAnnotationPresent(Id.class) && value == null) {
					//判定是否定义了注解的字段名
					String columnName = md.getAnnotation(Column.class).name();
					if (columnName != null && !"".equals(columnName)) {
						name = columnName;
					}
					value = StringUtil.getUUID();
				}
				if (value != null) {
					if (list.size() > 0) {
						sql.append(", ");
					}
					sql.append(name);
					if (value instanceof Boolean) {
						if ((Boolean) value) {
							list.add('Y');
						} else {
							list.add('N');
						}
					} else {
						list.add(value);
					}
				}
			} catch (Exception e) {
			}
		}
		if (!clz.equals(BaseEntity.class)) {
			dg_createInsert(obj, clz.getSuperclass(), sql);
		}
	}
	
	/**
	 * 执行处理
	 */
	public String createUpdate(Object obj, String table, String idName) {
		Class clz = obj.getClass();
		list = new ArrayList();
		StringBuilder sql = new StringBuilder();
		sql.append("update ").append(table).append(" set ");
		
		dg_createUpdate(obj, clz, sql, idName);
		
		if (whereValue == null) {
			return null;
		}
		sql.append(where);
		list.add(whereValue);
		return sql.toString();
	}
	/**
	 * 递归生成脚本
	 */
	private void dg_createUpdate(Object obj, Class clz, StringBuilder sql, String idName) {
		if (clz.equals(Object.class)) {
			return;
		}
		Field[] fields = clz.getDeclaredFields();
		String name = null;
		Object value = null;
		Method md = null;

		for (Field field : fields) {
			name = field.getName();
			try {
				md = clz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
				if (md.isAnnotationPresent(Transient.class)) {
					continue;
				}
				if (md.isAnnotationPresent(Column.class)) {
					//判定是否定义了注解的字段名
					String columnName = md.getAnnotation(Column.class).name();
					if (columnName != null && !"".equals(columnName)) {
						name = columnName;
					}
				}
				value = md.invoke(obj);
				if ((idName != null && name.toUpperCase().equals(idName.toUpperCase())) || 
						field.isAnnotationPresent(Id.class) || 
						md.isAnnotationPresent(Id.class)) {
					where = " where " + name + " = ?";
					whereValue = value;
					continue;
				}
				if (value != null) {
					if (list.size() > 0) {
						sql.append(", ");
					}
					sql.append(name).append("=?");
					if (value instanceof Boolean) {
						if ((Boolean) value) {
							list.add('Y');
						} else {
							list.add('N');
						}
					} else {
						list.add(value);
					}
				}
			} catch (Exception e) {
			}
		}
		if (!clz.equals(BaseEntity.class)) {
			dg_createUpdate(obj, clz.getSuperclass(), sql, idName);
		}
	}
	
	public List getParams() {
		return list;
	}
}